#include "softdelay.h"
#include "public.h"


static uint32_t usCnt = 1;
static uint32_t msCnt = 1;

void softdelay_init(void)
{
    uint32_t usBase = 72000000 >> 3;
    uint32_t usReams = 72000000 - (usBase << 3);
    uint32_t msBase = 72000000 / 8000;
    uint32_t msReams = 72000000 - (msBase * 8000);

    SystemCoreClockUpdate();
    usCnt = SystemCoreClock / usBase;
    msCnt = SystemCoreClock / msBase;

    if (usReams > (usBase >> 1))    usCnt++;
    if (msReams > (msBase >> 1))    msCnt++;
}
APP_INIT_EXPORT(softdelay_init);

void softdelay_us(uint32_t us)
{
    uint32_t i = us << usCnt;
    while (i--);
}

void softdelay_ms(uint32_t ms)
{
    uint32_t i = ms * msCnt;
    while (i--);
}

